$(function() {
	//设置根元素字体
	setFontSize(1080);

	var place = localStorage.place;
	$('.place').html(place + '元');
	var token = localStorage.phone;
	var sign_type = localStorage.sign_type;
	var school_id = localStorage.school_id;
	var sign_place = place;
	var payment_type = '';
	var ALIPAYSERVER = '';

	function goback() {
		history.back();
	}
	var wxChannel = null; // 微信支付
	var aliChannel = null; // 支付宝支付
	var channel = null;
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	mui.plusReady(function() {

		plus.payment.getChannels(function(channels) {
			aliChannel = channels[0];
			wxChannel = channels[1];
		}, function(e) {
			alert("获取支付通道失败：" + e.message);

		});
	})

	$("#butt").click(function() {
		$("input[name='payment_type']").each(function() {
			if($(this).prop('checked')) {
				payment_type = $(this).val();
			}

		});
		if(payment_type == '') {
			layer.msg("请选择支付方式");
		} else {
			$.ajax({
				url: "https://newapp.icandrive.cn/api/sign.php",
				data: {
					token: token,
					user_name: localStorage.examination_name,
					user_phone: localStorage.examination_no_phone,
					sign_type: sign_type,
					sign_place: sign_place,
					payment_type: payment_type,
					sign_type: localStorage.sign_type,
					school_id: localStorage.payschool_id
				},
				dataType: 'json',
				type: 'post',
				success: function(data) {

					if(data.status == 200) {
						localStorage.data_order_number = data.order_number;
						ALIPAYSERVER = 'https://newapp.icandrive.cn/api/alipay.php?total=' + sign_place + "&order_number=" + data.order_number;
						pay('alipay');

					} else {
						layer.msg("保存订单失败");
					}
				},
				error: function() {
					layer.msg('系统繁忙', function() {
						history.go(0)
					}, 1000);
				}
			});
			$(this).unbind();
		}

	})

	function pay(id) {
		console.log(ALIPAYSERVER)

		PAYSERVER = ALIPAYSERVER;
		channel = aliChannel;
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function() {
			switch(xhr.readyState) {
				case 4:
					if(xhr.status == 200) {
						plus.payment.request(channel, xhr.responseText, function(result) {
							//
							/*请求发送培训订单生产短信通知接口*/
							$.ajax({
								url: "https://newapp.icandrive.cn/api/school_sm.php",
								type: 'post',
								dataType: 'json',
								data: {
									order_number: localStorage.data_order_number
								},
								success: function() {
									/*localStorage.baoming = '报名'
									setInterval(function() {
										window.location.href = "../alert_info/alert_esc.html?type=" + '报名'
									}, 4000)*/
								}
							});
							localStorage.type = '报名';
							setInterval(function() {
								window.location.href = "../alert_info/alert_esc.html";
							}, 3000)

						}, function(error) {
							//plus.nativeUI.alert("支付失败：" + error.code);
							plus.nativeUI.alert("支付失败啦！");
						});
					} else {
						alert("获取订单信息失败！");
					}
					break;
				default:
					break;
			}
		}
		xhr.open('GET', PAYSERVER);
		xhr.send();
	}
})
